package com.yzt.redpack.dialog;

import static com.luck.picture.lib.utils.ToastUtils.showToast;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.yzt.redpack.R;
import com.yzt.zhuju.dialog.BottomBaseDialog;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.im.SendRedPkgEvent;
import com.yzt.zhuju.event.main.ExitAppEvent;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.dynamic.RedPkgDetailBean;
import com.yzt.zhuju.network.entity.message.RedBean;

import org.greenrobot.eventbus.EventBus;

/**
 * 作者：bobwang
 * 创建时间：2023-10-12 21:27:43
 * 描述：发布红包
 */
@SuppressLint("SetTextI18n")
public class RedPkgDialog extends BottomBaseDialog {

    private EditText et_count;
    private EditText et_msg;

    private String targetUserId;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_add_private_red_pkg;
    }

    @Override
    public void bindView(View v) {
        targetUserId = getArguments().getString("extra_user_id");
        et_count = findViewById(R.id.et_count);
        et_msg = findViewById(R.id.et_msg);
        initClick();
    }

    /**
     * 监听处理
     */
    private void initClick() {

        findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });

        findViewById(R.id.tv_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String count = et_count.getText().toString();
                String msg = et_msg.getText().toString();


                if (targetUserId == null) {
                    showToast(getContext(), "请选择发送对象");
                    return;
                }

                if (count.equals("")) {
                    showToast(getContext(), "请输入糖币数量");
                }

//                if (msg.equals("")) {
//                    showToast(getContext(), "请输入留言信息");
//                    return;
//                }

                postRed();
            }
        });
    }

    /**
     * 发红包
     */
    private void postRed() {
        String count = et_count.getText().toString();
        String msg = et_msg.getText().toString();
        JSONObject json = new JSONObject();
        json.put("candyCoin", count);
        json.put("scenarioType", 1);
        json.put("targetUserId", targetUserId);
        if (msg.equals("")){
            json.put("dsc", "大吉大利");
        }else{
            json.put("dsc", msg);
        }
        json.put("redType", false);
        BizLayer.getInstance().getDynamicModule().addRedPkg(json, new AresSubscriber<RedPkgDetailBean>() {
            @Override
            protected void onSuccess(RedPkgDetailBean bean) {
                // 发送红包消息
                SendRedPkgEvent messageContent = new SendRedPkgEvent();
                messageContent.setContent("「红包」");
                messageContent.setCandyCoin(count);
                messageContent.setRedPacketId(bean.getRedPackageId());
                messageContent.setDsc(msg);
                messageContent.setTargetUid(targetUserId);
                RxBus.getDefault().post(messageContent);
                showToast(getContext(), "红包发送成功");
                dismissAllowingStateLoss();
            }
        });
    }
}
